﻿using DevExpress.XtraBars.Alerter;
using EyFramework.Common;
using System;
using System.Windows.Forms;

namespace EyFramework.Library
{
    public class DevMsgUtil : IMsgUtil
    {
        private enum NOTICE_TYPE
        {
            NOTICE_INFO,
            NOTICE_WARNING,
            NOTICE_ERROR
        }

        private AlertControl alert = new AlertControl();

        /// <summary>
        /// 通知界面高度
        /// </summary>
        private int _noticeHeight = 90;

        /// <summary>
        /// 通知界面高度
        /// </summary>
        public int NoticeHeight
        {
            get { return _noticeHeight; }
            set { _noticeHeight = value; }
        }

        /// <summary>
        /// 通知界面宽度
        /// </summary>
        private int _noticeWidth = 250;

        /// <summary>
        /// 通知界面宽度
        /// </summary>
        public int NoticeWidth
        {
            get { return _noticeWidth; }
            set { _noticeWidth = value; }
        }

        public DevMsgUtil()
        {
            alert.AppearanceCaption.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            alert.AppearanceCaption.Options.UseFont = true;
            alert.AppearanceText.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            alert.AppearanceText.Options.UseFont = true;
            alert.FormShowingEffect = AlertFormShowingEffect.MoveHorizontal;
            alert.AutoFormDelay = 3000;
            alert.AllowHtmlText = true;
            alert.BeforeFormShow += new DevExpress.XtraBars.Alerter.AlertFormEventHandler(this.alert_BeforeFormShow);
            alert.FormLoad += new DevExpress.XtraBars.Alerter.AlertFormLoadEventHandler(this.alert_FormLoad);
        }

        public void ShowTips(string pMsg)
        {
            DevExpress.XtraEditors.XtraMessageBox.Show(pMsg, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public void ShowWarning(string pMsg)
        {
            DevExpress.XtraEditors.XtraMessageBox.Show(pMsg, "警告信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        public void ShowError(string pMsg)
        {
            DevExpress.XtraEditors.XtraMessageBox.Show(pMsg, "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public DialogResult ShowYesNoAndTips(string pMsg)
        {
            return DevExpress.XtraEditors.XtraMessageBox.Show(pMsg, "提示信息！",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Information,
                MessageBoxDefaultButton.Button2);
        }

        public DialogResult ShowYesNoAndWarning(string pMsg)
        {
            return DevExpress.XtraEditors.XtraMessageBox.Show(pMsg, "警告信息！",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning,
                MessageBoxDefaultButton.Button2);
        }

        DialogResult IMsgUtil.ShowYesNoAndWarning(Form pForm, string pMsg)
        {
            return ShowYesNoAndWarning(pForm, pMsg);
        }

        public static DialogResult ShowYesNoAndWarning(Form pForm, string pMsg)
        {
            return DevExpress.XtraEditors.XtraMessageBox.Show(pForm, pMsg, "警告信息！",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning,
                MessageBoxDefaultButton.Button2);
        }

        public DialogResult ShowYesNoAndError(string pMsg)
        {
            return DevExpress.XtraEditors.XtraMessageBox.Show(pMsg, "错误信息！",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Error,
                MessageBoxDefaultButton.Button2);
        }

        public DialogResult ShowYesNoCancelAndTips(string pMsg)
        {
            return DevExpress.XtraEditors.XtraMessageBox.Show(pMsg, "提示信息！",
                MessageBoxButtons.YesNoCancel,
                MessageBoxIcon.Information,
                MessageBoxDefaultButton.Button3);
        }

        DialogResult IMsgUtil.ConfirmYesNo(string pMsg)
        {
            return ConfirmYesNo(pMsg);
        }

        public static DialogResult ConfirmYesNo(string pMsg)
        {
            return DevExpress.XtraEditors.XtraMessageBox.Show(pMsg, "确认",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button2);
        }

        public DialogResult ConfirmYesNoCancel(string pMsg)
        {
            return DevExpress.XtraEditors.XtraMessageBox.Show(pMsg, "确认",
                MessageBoxButtons.YesNoCancel,
                MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button3);
        }

        public void ShowException(Exception ex)
        {
            FrmExceptionMsg frmException = new FrmExceptionMsg(ex);
            frmException.ShowDialog();
        }

        #region 通知相关

        private void alert_FormLoad(object sender, AlertFormLoadEventArgs e)
        {
            e.AlertForm.OpacityLevel = 0.9;//透明度
            e.AlertForm.Size = new System.Drawing.Size(_noticeWidth, _noticeHeight);
        }

        private void alert_BeforeFormShow(object sender, AlertFormEventArgs e)
        {
            e.AlertForm.OpacityLevel = 0.9;//透明度
            e.AlertForm.Size = new System.Drawing.Size(_noticeWidth, _noticeHeight);
        }

        public void ShowInfoNotice(Form owner, string pMsg, string pCaption = "")
        {
            ShowNotice(NOTICE_TYPE.NOTICE_INFO, owner, pMsg, pCaption);
        }

        public void ShowWarningNotice(Form owner, string pMsg, string pCaption = "")
        {
            ShowNotice(NOTICE_TYPE.NOTICE_WARNING, owner, pMsg, pCaption);
        }

        public void ShowErrorNotice(Form owner, string pMsg, string pCaption = "")
        {
            ShowNotice(NOTICE_TYPE.NOTICE_ERROR, owner, pMsg, pCaption);
        }

        private void ShowNotice(NOTICE_TYPE noticeType, Form owner, string message, string caption)
        {
            AlertInfo alertInfo = new AlertInfo(caption, message);
            switch (noticeType)
            {
                case NOTICE_TYPE.NOTICE_INFO:
                    alertInfo.Image = EyFramework.Library.Properties.Resources.notice;
                    alertInfo.Caption = string.IsNullOrEmpty(caption) ? "温馨提醒" : caption;
                    break;

                case NOTICE_TYPE.NOTICE_WARNING:
                    alertInfo.Image = EyFramework.Library.Properties.Resources.waring;
                    alertInfo.Caption = string.IsNullOrEmpty(caption) ? "系统警告" : caption;
                    break;

                case NOTICE_TYPE.NOTICE_ERROR:
                    alertInfo.Image = EyFramework.Library.Properties.Resources.wrong;
                    alertInfo.Caption = string.IsNullOrEmpty(caption) ? "系统错误" : caption;
                    break;
            }
            alert.Show(owner, alertInfo);
        }

        #endregion 通知相关
    }
}